package com.xj.edu.hotel.controller;

import com.xj.edu.hotel.domain.Hotel;
import com.xj.edu.hotel.domain.User;
import com.xj.edu.hotel.domain.Collect;
import com.xj.edu.hotel.service.CollectService;
import com.xj.edu.hotel.service.UserService;
import com.xj.edu.hotel.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/EditUser")
public class EditUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CollectService collectService;
    @Autowired
    private HotelService hotelService;
    /**
     * 用户信息查询
     */
    @RequestMapping("findUserId")
    public String findUserId(Model model, HttpSession session){
        User users = (User) session.getAttribute("user");
        User user = userService.findById(users.getUserId());
        model.addAttribute("user",user);
        return "editUser";
    }
    /**
     * 用户信息修改
     */
    @RequestMapping("editUser")
    public String editHotel(User user, HttpSession session) {
        User users = (User) session.getAttribute("user");
        user.setUserId(users.getUserId());
        int success = userService.editUser(user);
        return "redirect:/toUserLogin";
    }
    /**
     * 用户酒店收藏
     */
    @RequestMapping("collectHotel")
    @ResponseBody
    public String collectHotel(String id,Model model,HttpSession session){
        User user=(User) session.getAttribute("user");
        Hotel hotel = hotelService.findById(id);
        Collect collectY = collectService.findByHotelId(id);
        if(collectY == null){
            Collect collect = new Collect();
            collect.setUserId(user.getUserId());
            collect.setCityId(hotel.getCityId());
            collect.setFileContentType(hotel.getFileContentType());
            collect.setFileName(hotel.getFileName());
            collect.setHotelAddress(hotel.getHotelAddress());
            collect.setHotelName(hotel.getHotelName());
            collect.setHotePicPath(hotel.getHotePicPath());
            collect.setHotelId(hotel.getHotelId());
            collect.setHotelNumber(hotel.getHotelNumber());
            collect.setHotelType(hotel.getHotelType());
            int success = collectService.collectHotel(collect);
            model.addAttribute("hotel",hotel);
            return "ok";
        }else {
            return "error";
        }
    }


    /**
     * 查看用户收藏酒店
     */
@RequestMapping("/find")
    public String findCollect(Model model,HttpSession session) {
    User user = (User) session.getAttribute("user");
    List <Collect>collect = collectService.findByUserId(user.getUserId());
    model.addAttribute("collect", collect);
    return "collectHotel";
}

    /**
     *取消收藏
     */
    @RequestMapping("/removeCollect")
    public String removeCollect(String id){
      int success =  collectService.removeCollect(id);
      if (success > 0){
        return "redirect:/EditUser/find" ;
      }else {
          return "404";
      }
    }
    @RequestMapping("/allHotel")
   public String allHotel( Model model){
        List<Hotel> hotel=hotelService.findHotels();
        model.addAttribute("hotel",hotel);
        return "gallery";

    }
}


